// Copyright (c) 2012 SameGoal LLC.
// All Rights Reserved.
// Author: Andy Hochhaus <ahochhaus@samegoal.com>

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NET_HTTP_SERVER_EVENT_CONNECTION_H_
#define NET_HTTP_SERVER_EVENT_CONNECTION_H_

#include <event2/event.h>
#include <event2/listener.h>
#include <gflags/gflags.h>
#include <glog/logging.h>

#include <string>
#include <list>

#include "base/basictypes.h"
#include "net/http/server/event/event_server.h"
#include "net/http/server/event/request.h"
#include "util/worker_pool/worker_pool.h"

namespace sg {
namespace net {
namespace http {
namespace server {
namespace event {

class Connection {
 public:
  Connection(EventServer *server,
             sockaddr_t *address,
             bufferevent_t *buff_ev,
             bool encrypted);
  ~Connection();

  bool encrypted() const;

  void Init();
  std::string address();
  uint16 port();
  void set_read_timeout(const timeval_t &timeout);
  void set_write_timeout(const timeval_t &timeout);
  EventServer *event_server();
  std::recursive_mutex *mutex();
  void OutputReadyEvent(Request *req);
  void SetConnectionCloseHandler(Callback callback);
  void CloseConnectionAfterNextWrite();

 private:
  EventServer *server_;
  std::string address_;
  uint16 port_;
  bufferevent_t *buff_ev_;
  bool encrypted_;
  timeval_t *read_timeout_, *write_timeout_;
  std::list<Request *> requests_;
  // TODO(amarinph) HACK: deleting requests causes mutex and threading errors
  // (probably related to memory corruption). this is a quick fix to delay
  // request deletions until the dtor of Connection is invoked
  std::list<Request *> requests_to_delete_;
  std::recursive_mutex mutex_;
  bool closed_;
  Callback close_cb_;

  enum ConnectionMode { UNKNOWN, SINGLE, ORDERED, MULTIPLEXED, CLOSE };
  ConnectionMode connection_mode_;
  enum ConnectionReadState { READ_HEADERS, READ_BODY };
  ConnectionReadState connection_read_state_;

  static void ReadCallback(bufferevent_t *buff_ev, void *ctx);
  static void WriteCallback(bufferevent_t *buff_ev, void *ctx);
  static void EventCallback(bufferevent_t *buff_ev, int16 events, void *ctx);
  static void WriteOutput(evutil_socket_t fd, int16 events, void *ctx);

  bool ReadHeaders(Request *req, evbuffer_t *input);
  bool ReadBody(Request *req, evbuffer_t *input);
  bool CloseConnection();

  static ConnectionMode GetConnectionType(Request *req);

  DISALLOW_COPY_AND_ASSIGN(Connection);
};

}  // namespace event
}  // namespace server
}  // namespace http
}  // namespace net
}  // namespace sg

#endif  // NET_HTTP_SERVER_EVENT_CONNECTION_H_
